/*
 * process.c

 *
 *  Created on: May 14, 2013
 *      Author: Edward
 */
#include <stdlib.h>
#include <stdio.h>
#include "process.h"
#include "global.h"


/*
 * process Constructor.
 */
ProcessPtr newProcessConstructor(void) {
	ProcessPtr process  = (ProcessPtr) malloc(sizeof(ProcessStr));
	process->requests = (int*) malloc(sizeof(int));

	process->no_requests = 0;
	process->no_steps = 0;
	process->proc_type = COMPUTE;

    //Pointers to functions.
	process->setProcType = setProcType;
	process->getProcType = getProcType;
	process->setNoSteps = setNoSteps;
	process->getNoSteps = getNoSteps;
	process->setNoRequests = setNoRequests;
	process->getNoRequests = getNoRequests;
	process->setRequests = setRequests;
	process->getRequests = getRequests;
	process->toStringProc = toStringProc;

    return process;
}
/*
 * Add process request such as keyboard, io etc to request array
 * this - the ProcessPtr
 * request - the request of the process
 */
int setRequests(ProcessPtr this, int request) {
     this->requests[this->no_requests] = request;
	 return NO_ERROR;
}
/*
 * Return an array that has types of process requested
 * this - the ProcessPtr
 */
 int* getRequests(ProcessPtr this) {
	 int elements = sizeof(this->requests)/sizeof(this->requests[0]);
	 int * temp = (int*) malloc(sizeof(int) * elements);
	 int i;
	 for(i = 0; i < elements; i++){
		 temp[i] = this->requests[i];
	 }
	 return temp;
 }
/*
 * Set process number of instructions
 * this - the ProcessPtr
 */
int setNoSteps(ProcessPtr this) {
	this->no_steps++;
	 return NO_ERROR;
}
/*
  * Return process number of instructions
  * this - the PCBPtr
  */
 int getNoSteps(ProcessPtr this) {
	 return this->no_steps;
 }
/*
 * Set process type
 * this - the ProcessPtr
 */
int setProcType(ProcessPtr this, int type) {
	this->proc_type = type;
	 return NO_ERROR;
}
/*
  * Return process type
  * this - the ProcessPtr
  */
 int getProcType(ProcessPtr this) {
	 return this->proc_type;
 }
/*
 * Increment the request
 * this - the ProcessPtr
 */
int setNoRequests(ProcessPtr this) {
	this->no_requests++;
	 return NO_ERROR;
}
 /*
  * Return number of requests by process
  * this - the ProcessPtr
  */
 int getNoRequests(ProcessPtr this) {
	 return this->no_requests;
 }
 /*
  * Object representation of the PCB
  */
int toStringProc(ProcessPtr this){
	char * state;
	switch(this->proc_type){
	case COMPUTE: state = "compute";
	break;
	case IO: state = "io";
	break;
	case KEYBOARD: state = "keyboard";
	break;
	default: state = "unknown";
	break;
	}
	printw("Process TYPE:%s REQUEST:%d STEP:%d", state, this->no_requests,
			this->no_steps);
	return 0;
}

//int main(void)
//{
//	ProcessPtr proc = newProcessConstructor();
//	proc->setRequests(proc, 4);
//	proc->setNoRequests(proc);
//
//	proc->setRequests(proc, 45);
//	proc->setNoRequests(proc);
//
//	proc->setNoSteps(proc);
//
//    int * tmp = NULL;
//        int i;
//        tmp =  proc->getRequests(proc);
//        for(i = 0; i < proc->no_requests; i++){
//        	    	printw("%d ",tmp[i]);
//        }
//
//
//
//	proc->toStringProc(proc);
//
//	return 0;
//}


